var LOG = LOG ||{};
/**
 * 日志管理-登陆管理
 */
LOG.userLogin = Ext.extend(Ext.Panel,{
	closable:true
	,width:'100%'
	,initComponent:function(){
		this.tbar = this.buildBbar(this);
		var gp = new LOG.userLogin.gridPanel();
		this.items = [gp];
		this.formPanel = new LOG.userLogin.searchPanel({thisGrid:gp});
		LOG.userLogin.superclass.initComponent.call(this);
	}
	,buildBbar: function(mp) {
   		return [{
   				text: "按条件查询",
   				handler:function(btn){
   					var tform = btn.ownerCt.ownerCt.formPanel;
   	    			tform.show();
   				},
    			iconCls: 'ico-dot-search'
	   		}
   			,{xtype:'spacer',width:30}
   			,{
   				text: "删除日志",
   				handler:this.deleteLog,
    			iconCls: 'ico-task-delete'
	   		}
//   			,{
//   				text: "数据导出",
//   				handler:this.exportData,
//    			iconCls: 'ico-dot-report'
//	   		}
   			] ;
   	}
	,deleteLog:function(btn) {
   		var gp = btn.ownerCt.ownerCt.items.get(0) ;
   		var rows= gp.getSelectionModel().getSelections(); 
   		if(rows.length==0) {
   			 Ext.Msg.alert("提示", "请选择要操作的日志!");
   			 return ;
   		}
   		var ids = "";
   		for(var i=0 ;i<rows.length;i++) {
   			if(i==0) {
   				ids = rows[i].data['ID'] ;
   			}else{
   				ids = ids +","+rows[i].data['ID'] ;
   			}
   		}
   		Ext.MessageBox.confirm("确认", "确认要删除这些日志吗?", function(btn) {
					if (btn == "yes") {
						Ext.Ajax.request( {
							waitMsg : '正在执行  ',
							method : 'POST',
							params : {
								ids:ids 
							},
							url : 'log_delSystemLogs.action',
							scope : this,
							success : function(request) {
								var Result=Ext.decode(request.responseText);
								if(Result.success) {
									gp.getStore().reload();
								} else {
									 Ext.Msg.alert("错误",Result.info );
								}
							},
							failure : function() {
								Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
							}
						});
						
					}
				});
   	}
});

LOG.userLogin.conditions = "";

LOG.userLogin.gridPanel = Ext.extend(Ext.grid.GridPanel,{
	initComponent : function(){
		var thisGrid = this;
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
					dataIndex: "ID",
	                sortable: false,
	                singleSelect:false,
	                width:20
				});
   	 	this.recordStart = 1 ;
		var rn = new　Ext.grid.RowNumberer({
		　　header　:　"序号",
		　　width　:　35,
		　　renderer:function(value,metadata,record,rowIndex){
		　　　return　　thisGrid.recordStart　+　rowIndex;
		　　}
		});
   	    var userLoginStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'log_getSystemLogs.action'
        	 ,fields:[ 'ID','LOGIN_TIME','USER_NAME','IP','LOG_CONTENT','LOGOUT_TIME']
        	 ,root:'root'
        });
   	    this.viewConfig = {
   	    	forceFit:true
   	    };
   	    this.store = userLoginStore;
		this.sm = selectMode;
		this.bbar = [new Ext.PagingToolbar({
					pageSize : 200,
					store : userLoginStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							thisGrid.recordStart = obj.start+1 ;  
						}
					}
				})];
		this.columns = [selectMode
					,{
				        header:'用户名',
						sortable:true,
						dataIndex:"USER_NAME",
						width:50
					},{
				        header:'登录时间',
						sortable:true,
						dataIndex:"LOGIN_TIME",
						width:50
					},{
				        header:'登出时间',
						sortable:true,
						dataIndex:"LOGOUT_TIME",
//						renderer:function(value, metaData, record, rowIndex, colIndex, store){
//							return value;
//						},
						width:50
					},{
				        header:'状态',
						sortable:true,
						dataIndex:"LOG_CONTENT",
//						renderer:function(value, metaData, record, rowIndex, colIndex, store){
//							if(value == null){
//								return "";
//							}
//						},
						width:50
					},{
				        header:'登录IP',
						sortable:true,
						dataIndex:"IP",
						width:50
					}
			];
		userLoginStore.load({
			params:{
				start:0,
				limit:200
			}
		});
		LOG.userLogin.gridPanel.superclass.initComponent.call(this);
	}
});

LOG.userLogin.formPanel = Ext.extend(Ext.form.FormPanel,{
		text:"表单",
		layout:"form",
		region : "center",
		labelAlign:"right",
		width:550,
		height:300,
		frame:true,
		initComponent:function(){

				this.items=[
							{fieldLabel : '开始日期：',
								name : 'startTime',
								xtype:"datefield",
								editable : true,
								format:'Y-m-d',
								emptyText:"请选择",
								anchor:"90%"
					    	}
							,{fieldLabel : '结束日期：',
								name : 'endTime',
								xtype:"datefield",
								editable : true,
								format:'Y-m-d',
								emptyText:"请选择",
								anchor:"90%"
					    	}
							,{fieldLabel : '员工名称：',
								xtype:"searchfield",
								anchor:"90%",
								name:"staffId",
								passName:'staffIdPass',
//								readOnly: true,
//								emptyText:'请选择',
								setPassValue:function(rec){
											if(rec) {
												if(rec.data){
													if(rec.data['STAFF_ID']) {
														this.passField.value = rec.data['STAFF_ID'];
														this.setValue(rec.data['STAFF_NAME']);
													}
													
												}
											}
								},
								OnFindSearch:function(f) {
									new LOG.userLogin.validStaff({textField:this}).show();
								}
					    	}
			];
			LOG.userLogin.formPanel.superclass.initComponent.call(this);
		}
});
/**
 * 查询面板
 */
LOG.userLogin.searchPanel = Ext.extend( Ext.Window,{
   		closeAction: 'hide',
		resizable:false,
		modal:true,
		width:550,
		height:350
   		,layout:"fit"
   		,title: "按条件登录日志"
   		,initComponent:function(){
			this.items = [new LOG.userLogin.formPanel()];
			LOG.userLogin.searchPanel.superclass.initComponent.call(this);
   		}
   		,buttonAlign :'center'
		,minButtonWidth:60
		,buttons:[{
			text:"确定",
			handler : function(btn){
				var a = btn.ownerCt.ownerCt.items.get(0);
			    var thisForm = a.getForm();
			    var startTime  = thisForm.findField('startTime').getValue();
			    	startTime = startTime?startTime.format('Y-m-d'):"";
			    var endTime = thisForm.findField('endTime').getValue();
				    endTime = endTime?endTime.format('Y-m-d'):"";
			    var staffId = thisForm.findField('staffId').passField.value;
			    var store = btn.ownerCt.ownerCt.thisGrid.getStore();
			    
//			    //保存条件
//			    LOG.userLogin.conditions =
//			    	'tsLOGuserLogin.eparchyCode='+eparchyCode+
//					'&tsLOGuserLogin.cityCode='+cityCode+
//					'&tsLOGuserLogin.startDate='+statDay+
//					'&tsLOGuserLogin.checkDate='+checkDate;
			    
			    btn.ownerCt.ownerCt.thisGrid.recordStart = 1;
			    store.baseParams = {
					'userName':staffId, 'startTime':startTime, 'endTime':endTime
				};
	
	            store.load({params:{start:0, limit:200}});
			    
			   btn.ownerCt.ownerCt.hide();//隐藏弹出页面
			}
		}, {
			text : '重置',
			handler : function() {this.ownerCt.ownerCt.form.reset();}
		}, {
			text : '取消',
			handler : function() {this.ownerCt.ownerCt.hide();}
		}]
});

/**
 * 有效员工查询
 */
LOG.userLogin.validStaff = Ext.extend(Ext.Window,{
	width:650
	,height:400
	,layout:'border'
	,title:'员工列表'
	,plain:true
	,modal:true
	,closeAction:'close'
	,limit:25
	,resizable:true
	,initComponent:function(){
		this.tbar = this.buildTbar(this);
		this.items = this.buildGrid();
		this.buttons = this.buildButton();
		LOG.userLogin.validStaff.superclass.initComponent.call(this);
	}
	,buildTbar: function(mp){
		return [{xtype:'spacer',width:10}
			,{xtype:'label',text:'员工姓名：'}
			,{
				xtype:'textfield'
				,width:200
				,name:'staffName'
				,emptyText:'请输入员工姓名'
				,ref: '../searchStaffText'
				,listeners:{
                 	 	'specialkey':function(f,e) {
                 	 		if(e.getKey() == e.ENTER){
								mp.doSearch(mp.searchStaffText);
                 	 		}
                 	 	}
                 	 }
			},'-'
			,{xtype:'spacer',width:10}
			,{
   				text : "查询",
   				ref:"../search",
   				handler :this.doSearch,
   				iconCls: 'ico-dot-search'
   			 }];
	}
	,buildGrid: function(){
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
			dataIndex: "ID",
            sortable: false,
            singleSelect:true,
            width:20
		});
   	    var staffStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'log_getValidStaff.action'
        	 ,fields:['STAFF_ID','STAFF_NAME']
        	 ,root:'root'
        });
		return[{
			xtype:'grid'
			,region:'center'
			,viewConfig:{
				forceFit:true
			}
			,store:staffStore
			,sm:selectMode
			,bbar:[new Ext.PagingToolbar({
					pageSize : 200,
					store : staffStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							staffStore.recordStart = obj.start+1 ;
						    
						}
					}
				})]
			,columns:[selectMode
				    ,{
				        header:'用户id',
						sortable:true,
						dataIndex:"STAFF_ID",
					}
					,{
				        header:'用户名',
						sortable:true,
						dataIndex:"STAFF_NAME",
					}
			]
		}];
	}
	,buildButton: function(){
		return [{
            text:'确认',
            listeners:{
         		click:function(btn) {
						var gp = btn.ownerCt.ownerCt.items.get(0) ;
   						var rows= gp.getSelectionModel().getSelections();
   						
   						if(rows.length==0) {
				   			 Ext.Msg.alert("提示", "请选择查询的员工id!");
				   			 return ;
				   		}else{
				   			var a = gp.ownerCt.textField;
				   			a.setPassValue(rows[0]);
//				   			gp.ownerCt.textField.setValue(rows[0].data['STAFF_ID']);
				   		}
   						
         				this.ownerCt.ownerCt.close();
         			}
         		}
            }
            ,{
             text:'关闭',
             listeners:{
             	click:function(n) {
             		this.ownerCt.ownerCt.close();
             	}
             }
            }
		];
	}
	,doSearch:function(btn){
//		alert(arguments[0]);
   		var text = btn.ownerCt.ownerCt.searchStaffText.getValue();
   		var store = btn.ownerCt.ownerCt.items.get(0).getStore();
   		
   		store.baseParams = {'userName':text} ;
   		store.load({
			params:{
				start:0,
				limit:200
			}
		});
	}
});